package com.example.book.service;

import com.example.book.enums.BookStatus;
import com.example.book.mapper.BookInfoMapper;
import com.example.book.model.BookInfo;
import com.example.book.model.PageInfo;
import com.example.book.model.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookService {
    //
    @Autowired
    private BookInfoMapper bookInfoMapper;
    //
    public PageResult<BookInfo> getBookListByPage(PageInfo pageInfo) {
        Integer count = bookInfoMapper.count();
        List<BookInfo> bookInfos = bookInfoMapper.queryBookByPage(pageInfo.getOffset(), pageInfo.getPageSize());
        for(BookInfo bookInfo : bookInfos) {
            bookInfo.setStatusCN(BookStatus.getDescribeByCode(bookInfo.getStatus()).getDescribe());
        }
        return new PageResult<>(bookInfos, count,pageInfo);
    }

    public Integer addBook(BookInfo bookInfo) {
        return bookInfoMapper.addBook(bookInfo);
    }

    public BookInfo selectBookById(Integer id) {
        BookInfo bookInfo = bookInfoMapper.selectBookById(id);
        bookInfo.setStatusCN(BookStatus.getDescribeByCode(bookInfo.getStatus()).getDescribe());
        return bookInfo;
    }

    public Integer updateBookById(BookInfo bookInfo) {
        return bookInfoMapper.updateBookById(bookInfo);
    }

    public Integer deleteBookById(Integer id) {
        BookInfo bookInfo = new BookInfo();
        bookInfo.setId(id);
        bookInfo.setStatus(0);
        return bookInfoMapper.updateBookById(bookInfo);
    }

    public Integer batchDeleteByID(List<Integer> ids) {
        return bookInfoMapper.batchDeleteByID(ids);
    }
}
